#!/usr/bin/Rscript

#$ID$

#call: R --vanilla --slave < pondSim.bargraph.R



#read data file
foo       <- scan('pondSim.output',what=list(character(0),double(0),double(0),double(0),double(0),double(0)),skip=1)
names     <- foo[[1]][1:11][-c(7,8)]
bottom    <- foo[[2]][1:11][-c(7,8)]
berm      <- foo[[3]][1:11][-c(7,8)]
initialHt <- foo[[4]][1:11][-c(7,8)]
maxHt     <- foo[[5]][1:11][-c(7,8)]
finalHt   <- foo[[6]][1:11][-c(7,8)]

try(library(Cairo))
try(library(Cairo,lib.loc='/home/acvmarsh/R/i686-pc-linux-gnu-library/2.8/'))

#create strings for text on plot
inithtstr=as.character(initialHt)
inithtstr=substr(inithtstr,1,4)
finalhtstr=as.character(finalHt)
finalhtstr=substr(finalhtstr,1,4)

#specify colors
clrs = c(colors()[379],colors()[149],colors()[633],colors()[125])

#create bargraph
#PNG
sink('/dev/null')
png(file='barplot.png',width=800,height=400,pointsize=17)
#CairoPNG(file='barplot.png',width=800,height=400,pointsize=15,font=1)
    barplot(height=berm,col='lightgray',width=3,space=.333,border=F,ylim=c(0,10))
    barplot(names.arg=names,
        height=rbind(initialHt,maxHt,finalHt),
        col=clrs[-1],
        ylab='Elevation (ft above MSL)',
        cex.names=.7,
        beside=TRUE,
        border=NA,
        add=T)
    #uncomment next line to display initial heights
    text(labels=inithtstr,x=seq(from=1,by=4,length.out=9),y=initialHt+.1,pos=4,srt=70,font=2)
    text(labels=finalhtstr,x=seq(from=3,by=4,length.out=9),y=finalHt+.1,pos=4,srt=70,font=2)
    legend(x=8,y=9.5,legend=c('Berm','Initial','Max','Final'),fill=clrs,horiz=T)
dev.off()

#PDF
pdf(file='barplot.pdf',width=10,height=5)
    barplot(height=berm,col='lightgray',width=3,space=.333,border=F,ylim=c(0,9))
    barplot(names.arg=names,
        height=rbind(initialHt,maxHt,finalHt),
        col=clrs[-1],
        ylab='Elevation (ft above MSL)',
        cex.names=.7,
        beside=TRUE,
        border=NA,
        add=T)
    #uncomment next line to display initial heights
    text(labels=inithtstr,x=seq(from=1,by=4,length.out=9),y=initialHt+.1,pos=4,srt=70)
    text(labels=finalhtstr,x=seq(from=3,by=4,length.out=9),y=finalHt+.1,pos=4,srt=70)
    legend(x=9,y=8.5,legend=c('Berm','Initial','Max','Final'),fill=clrs,horiz=T)
dev.off()
sink()

#system('evince barplot.pdf &')
